<?php
/**
 * 团购管理
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-05-01 13:02:01
 */
namespace app\bis\controller;

use think\Controller;
use think\Request;

class Deal extends Controller
{

    public function index()
    {
        // 列表请自行完成
        return '团购列表页面请自行完成！';
    }

    // 团购商品添加
    public function add(Request $request)
    {
        // 获取商户id
        $bisId = session('bisAccount','','bis')['bis_id']?session('bisAccount','','bis')['bis_id']:0;
        $accId = session('bisAccount','','bis')['id']?session('bisAccount','','bis')['id']:0;
        if ($request->isPost()) {
            $data = $request->post();
            // 进行入库校验 validate
            // $vali = $this->validate($data, 'Deal.add');
            // if (TRUE !== $vali) {
            //     $this->error($vali);
            // }
            // dump($data);exit;

            $data['se_category_id'] = empty($data['se_category_id'])?'':implode(',', $data['se_category_id']);
            $data['location_ids'] = empty($data['location_ids'])?'':implode(',', $data['location_ids']);
            $location = model('BisLocation')->get($data['location_ids'][0]);
            $deals = [
                'name'               => $data['name'],
                'bis_id'             => $bisId,
                'category_id'        => $data['category_id'],
                'se_category_id'     => $data['se_category_id'],
                'city_id'            => $data['city_id'],
                'total_count'        => $data['total_count'],
                'location_ids'       => $data['location_ids'],
                'origin_price'       => $data['origin_price'],
                'current_price'      => $data['current_price'],
                'coupons_begin_time' => strtotime($data['coupons_begin_time']),
                'coupons_end_time'   => strtotime($data['coupons_end_time']),
                'image'              => $data['image'],
                'description'        => $data['description'],
                'start_time'         => strtotime($data['start_time']),
                'end_time'           => strtotime($data['end_time']),
                'xpoint'             => $location->xpoint,
                'ypoint'             => $location->ypoint,
                'bis_account_id'     => $accId,
                'notes'              => $data['notes'],
            ];

            $res = model('Deal')->add($deals);
            if (!$res) {
                $this->error('团购数据提交有误！');
            }
            $deals = [];
            $this->success('添加成功！', 'deal/index');
        }
        // 获取一级城市
        $citys = model('city')->getNormalFirstCity();
        // 获取一级栏目
        $categorys = model('category')->getNormalFirstCategory();
        // 获取门店
        $bislocations = model('BisLocation')->getNormalLocatByBisId(1);
        $this->assign('citys', $citys);
        $this->assign('categorys', $categorys);
        $this->assign('bislocations', $bislocations);
        return $this->fetch();
    }

}
